<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class Search extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        //获取文章的推荐标签
        Base::recommend('article');
    }


    public function index(Request $request)
    {

        //每页显示多少条
        $limit = 1;
        //当前是第几页
        $page = 1;

        //查询条件
        $map['title'] = ['like', '%' . $request->param('search_name') . '%'];

        //每页显示条数
        $limit = $request->isGet() && $request->has('limit') ? $request->param('limit') : $limit;
        //是否有参数  当前是第几页
        $page = $request->isGet() && $request->has('page') ? $request->param('current_page') : $page;


        $data = Db::name('Article')->where($map)->order(['id'=>'asc','create_time'=>'desc','hits'=>'desc'])->paginate($limit, FALSE, [
            'page' => $page,
            'query' => [$map],
        ])->toArray();

        $data['limit'] = $limit;
//        halt($data);
        $this->assign(['data' => $data]);


        return $this->fetch('list');
    }
}
